# encoding: utf-8
class Report::CulturalInstitutionReportController < ApplicationController
  def show
    @title_view = 'Reporte de Patrimonios por Institución'
    @cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>{:deleted=>[0]})
    @tipologies = CulturalHeritage::Tipology.find(:all,:conditions=>{:deleted=>[0]})
    @cultures = CulturalHeritage::Culture.find(:all,:conditions=>{:deleted=>[0]})
  end

  def show_pdf
    selected_cultural_institution = params[:cultural_institution][:id]
    start_date = params[:calendar][:start_date]
    end_date = params[:calendar][:end_date]
    tipology = params[:tipology][:id]
    culture = params[:culture][:id]

    pdf = Prawn::Document.new

    pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de información cultural pukara",
      :at => [100, 715],
      :width => 430
    pdf.text_box "Reporte de patrimonios por institución cultural",
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20

    pdf.move_down 140

    my_institution_conditions = {:deleted=>[0]}

    if ! selected_cultural_institution.blank?
      my_institution_conditions.update({:id=>[selected_cultural_institution]})
    else

    end

    @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>my_institution_conditions)
    @cultural_heritage_cultural_institutions.each {
      |cultural_heritage_institution|

      arr = Array.new


      pdf.text cultural_heritage_institution.name, :style => :bold, :size => 14, :align => :center
      pdf.move_down 10

      #Colocando headers
      arr[0] = Array.new
      arr[0][0] = "Patrimonio cultural"
      arr[0][1] = "Tipología"
      arr[0][2] = "Cultura"
      arr[0][3] = "Fecha de registro"

      j = 1

      my_heritage_conditions  = String.new
      my_heritage_values      = Array.new

      my_heritage_conditions << "deleted=? AND culturalInstitution=?"
      my_heritage_values.push(0)
      my_heritage_values.push(cultural_heritage_institution.id)

      if ! start_date.blank?
        my_heritage_conditions << " AND created_at >= ?"
        my_heritage_values.push(start_date.to_date)
      end

      if ! end_date.blank?
        my_heritage_conditions << " AND created_at <= ?"
        my_heritage_values.push(end_date.to_date)
      end

      if ! tipology.blank?
        my_heritage_conditions << " AND tipology=?"
        my_heritage_values.push(tipology)
      end

      if ! culture.blank?
        my_heritage_conditions << " AND culture=?"
        my_heritage_values.push(culture)
      end

      my_heritage_values.insert(0, my_heritage_conditions)

      @list_cultural_heritage = CulturalHeritage::CulturalHeritage.find(:all,:conditions=> my_heritage_values)
      if (@list_cultural_heritage.length == 0)
        pdf.text "No se encontraron patrimonios según los criterios elegidos.."
      end

      @list_cultural_heritage.each { |cultural_heritage|
        arr[j] = Array.new
        arr[j][0] = cultural_heritage.name
        if !(cultural_heritage.obj_tipology == nil)
          arr[j][1] = cultural_heritage.obj_tipology.name
        else
          arr[j][1] = "No especificado"
        end

        if !(cultural_heritage.obj_culture == nil)
          arr[j][2] = cultural_heritage.obj_culture.name
        else
          arr[j][2] = "No especificado"
        end

        arr[j][3] = cultural_heritage.created_at.strftime("%d/%m/%Y").to_s
        j = j + 1
      }

      if (arr.length > 1)
        pdf.table arr, :column_widths => [170, 130, 130, 110], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
          row(0).border_width = 2
          row(0).font_style = :bold
        end

        pdf.move_down 10

        if (arr.length == 2)
          registro = " registro."
        else
          registro = " registros."
        end

        pdf.text "Total: " << (arr.length - 1).to_s << registro
      end

      pdf.move_down 30

    }

    pdf.text "Criterios del reporte:", :style => :bold, :size => 14, :align => :left
    pdf.move_down 10

    arr = Array.new
    arr[0] = Array.new
    arr[1] = Array.new
    arr[2] = Array.new
    arr[3] = Array.new
    arr[4] = Array.new
    arr[5] = Array.new
    arr[0][0] = "Inicio de registro"
    arr[1][0] = "Fin de registro"
    arr[2][0] = "Institución cultural"
    arr[3][0] = "Tipología"
    arr[4][0] = "Cultura"
    arr[5][0] = "Total de registros"

    if start_date.blank?
      arr[0][1] = "No especificado"
    else
      arr[0][1] = start_date.to_s
    end

    if end_date.blank?
      arr[1][1] = "No especificado"
    else
      arr[1][1] = end_date.to_s
    end

    if selected_cultural_institution.blank?
      arr[2][1] = "Todas"
    else
      arr[2][1] = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>{:id=>[selected_cultural_institution]}).at(0).name
    end

    if tipology.blank?
      arr[3][1] = "Todas"
    else
      arr[3][1] = CulturalHeritage::Tipology.find(:all,:conditions=>{:id=>[tipology]}).at(0).name
    end

    if culture.blank?
      arr[4][1] = "Todas"
    else
      arr[4][1] = CulturalHeritage::Culture.find(:all,:conditions=>{:id=>[culture]}).at(0).name
    end

    my_heritage_conditions  = String.new
    my_heritage_values      = Array.new

    my_heritage_conditions = "deleted=?"
    my_heritage_values.push(0)

    if ! selected_cultural_institution.blank?
      my_heritage_conditions << " AND culturalInstitution=?"
      my_heritage_values.push(selected_cultural_institution)
    end

    if ! start_date.blank?
      my_heritage_conditions << " AND created_at >= ?"
      my_heritage_values.push(start_date.to_date)
    end

    if ! end_date.blank?
      my_heritage_conditions << " AND created_at <= ?"
      my_heritage_values.push(end_date.to_date)
    end

    if ! tipology.blank?
      my_heritage_conditions << " AND tipology=?"
      my_heritage_values.push(tipology)
    end

    if ! culture.blank?
      my_heritage_conditions << " AND culture=?"
      my_heritage_values.push(culture)
    end

    my_heritage_values.insert(0, my_heritage_conditions)

    @list_to_count_cultural_heritage = CulturalHeritage::CulturalHeritage.find(:all,:conditions=> my_heritage_values)
    arr[5][1] = @list_to_count_cultural_heritage.length

      if (arr.length > 1)
      pdf.table arr, :column_widths => [170, 170], :row_colors => ["F0F0F0", "FFFFFF"] do
        row(5).border_width = 2
        row(5).font_style = :bold
      end
      pdf.move_down 10
    end

    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [450, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end

    send_data pdf.render, :filename => "Reporte patrimonio por institucion " << Time.now.strftime("%d-%m-%Y").to_s << ".pdf", :type => "application/pdf"
  end
end
